﻿using System;
using System.Drawing;
using System.Web.UI.WebControls;
using Me.Amon.Da.Db;
using Me.Amon.Da.Db.POS;
using Me.Amon.POS.C;
using Me.Amon.Util;

namespace Me.Amon.POS
{
    public partial class Emps : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                return;
            }

            var user = WebUtil.CurrentUser(Session);
            if (user.Rank < IUser.LEVEL_01)
            {
                Response.Redirect(IEnv.HOME_PAGE);
                return;
            }

            LbActor.Items.Add(new ListItem { Text = "导购员", Value = CEmp.ACTOR_3.ToString() });
            LbActor.Items.Add(new ListItem { Text = "收银员", Value = CEmp.ACTOR_4.ToString() });
            LbActor.Items.Add(new ListItem { Text = "账务员", Value = CEmp.ACTOR_5.ToString() });
            LbActor.Items.Add(new ListItem { Text = "店长", Value = CEmp.ACTOR_6.ToString() });
            LbActor.Items.Add(new ListItem { Text = "技术员", Value = CEmp.ACTOR_8.ToString() });

            var act = Request["act"];
            if (!CharUtil.IsValidateHash(act))
            {
                act = CEmp.ACTOR_3.ToString();
            }
            LbActor.SelectedValue = act;

            var dba = new RDBEngine();
            ListEmp(dba, user.CorpId, act, 0);
        }

        protected void LbAct_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListEmp(new RDBEngine(), WebUtil.CurrentUser(Session).CorpId, LbActor.SelectedValue, 0);
        }

        #region 导航
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BnAppendEmp_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/POS/UpdateEmp.aspx?act=" + LbActor.SelectedValue);
        }
        #endregion

        private void ListEmp(RDBEngine dba, string corp, string act, int page)
        {
            dba.AddTable(DEmp.TABLE);
            dba.AddColumn(DEmp.ID);
            dba.AddColumn(DEmp.CODE);
            dba.AddColumn(DEmp.USER);
            dba.AddColumn(DEmp.DISPLAY_NAME);
            dba.AddColumn(DEmp.ACTOR);
            dba.AddColumn(DEmp.SEX);
            dba.AddColumn(DEmp.BIRTHDAY);
            dba.AddColumn(DEmp.CELLPHONE);
            dba.AddColumn(DEmp.TELEPHONE);
            dba.AddColumn(DEmp.ENGAGE_DATE);
            dba.AddWhere(DEmp.CORP_ID, corp);
            dba.AddWhere(DEmp.CHANNEL, CEmp.CHANNEL);
            dba.AddWhere(DEmp.ACTOR, act);

            var table = dba.ExecuteSelect();
            int cnt = table.Rows.Count;
            //if (cnt < 1)
            //{
            //    RpVips.DataSource = table;
            //    RpVips.DataBind();
            //    return;
            //}

            // 每页行数
            int RPP = 20;
            int num = (cnt - 1) / RPP;
            int p = page - 1;
            if (p + 2 > num)
            {
                p = num - 2;
            }
            if (p < 0)
            {
                p = 0;
            }

            HlHome.Enabled = num > 0 && page > 0;
            LlPrev.Visible = num > 3 && page > 0;

            HlPage1.CommandArgument = p.ToString();
            HlPage1.ForeColor = p == page ? Color.Red : Color.Blue;
            p += 1;
            HlPage1.Text = p.ToString();

            if (num > 0)
            {
                HlPage2.Visible = true;
                HlPage2.CommandArgument = p.ToString();
                HlPage2.ForeColor = p == page ? Color.Red : Color.Blue;
                p += 1;
                HlPage2.Text = p.ToString();
            }
            else
            {
                HlPage2.Visible = false;
            }

            if (num > 1)
            {
                HlPage3.Visible = true;
                HlPage3.CommandArgument = p.ToString();
                HlPage3.ForeColor = p == page ? Color.Red : Color.Blue;
                p += 1;
                HlPage3.Text = p.ToString();
            }
            else
            {
                HlPage3.Visible = false;
            }

            LlNext.Visible = num > 3 && page < num - 3;
            HlLast.Enabled = page < num;
            HlLast.CommandArgument = num.ToString();
            LlPage.Text = string.Format("第 {0}/{1} 页", page + 1, num + 1);

            PagedDataSource pds = new PagedDataSource();
            pds.DataSource = table.DefaultView;
            pds.AllowPaging = true;//允许分页
            pds.PageSize = RPP;//单页显示项数
            pds.CurrentPageIndex = page;

            RpVips.DataSource = pds;
            RpVips.DataBind();
        }

        #region 分页
        protected void HlHome_Click(object sender, EventArgs e)
        {
            ListEmp(new RDBEngine(), WebUtil.CurrentUser(Session).CorpId, LbActor.SelectedValue, 0);
        }

        protected void HlPage1_Click(object sender, EventArgs e)
        {
            int max = int.Parse(HlPage1.CommandArgument);
            ListEmp(new RDBEngine(), WebUtil.CurrentUser(Session).CorpId, LbActor.SelectedValue, max);
        }

        protected void HlPage2_Click(object sender, EventArgs e)
        {
            int max = int.Parse(HlPage2.CommandArgument);
            ListEmp(new RDBEngine(), WebUtil.CurrentUser(Session).CorpId, LbActor.SelectedValue, max);
        }

        protected void HlPage3_Click(object sender, EventArgs e)
        {
            int max = int.Parse(HlPage3.CommandArgument);
            ListEmp(new RDBEngine(), WebUtil.CurrentUser(Session).CorpId, LbActor.SelectedValue, max);
        }

        protected void HlLast_Click(object sender, EventArgs e)
        {
            int max = int.Parse(HlLast.CommandArgument);
            ListEmp(new RDBEngine(), WebUtil.CurrentUser(Session).CorpId, LbActor.SelectedValue, max);
        }
        #endregion
    }
}